// pages/index/index.js
const app = getApp();

Page({
  data: {
    userInfo: null,
    hasUserInfo: false,
    canIUseGetUserProfile: false,
    animationData: {}
  },

  onLoad() {
    // 检查是否可以使用getUserProfile
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      });
    }
    
    // 启动标题动画
    this.startTitleAnimation();
  },

  onShow() {
    // 页面显示时的处理
  },

  startTitleAnimation() {
    const animation = wx.createAnimation({
      duration: 2000,
      timingFunction: 'ease-in-out',
      delay: 0,
      transformOrigin: '50% 50%'
    });
    
    animation.scale(1.1).rotate(5).step();
    animation.scale(1).rotate(0).step();
    
    this.setData({
      animationData: animation.export()
    });
    
    // 循环动画
    setTimeout(() => {
      this.startTitleAnimation();
    }, 4000);
  },

  getUserProfile() {
    wx.getUserProfile({
      desc: '用于完善游戏体验',
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        });
        app.globalData.userInfo = res.userInfo;
      }
    });
  },

  startGame() {
    wx.navigateTo({
      url: '/pages/levels/levels'
    });
  },

  quickStart() {
    wx.navigateTo({
      url: `/pages/game/game?level=${app.globalData.currentLevel}`
    });
  },

  showRules() {
    wx.showModal({
      title: '游戏规则',
      content: '1. 点击相邻的相同颜色方块来消除\n2. 连接3个或更多相同方块可消除\n3. 消除更多方块获得更高分数\n4. 达到目标分数即可过关',
      showCancel: false,
      confirmText: '我知道了'
    });
  },

  toggleSound() {
    app.globalData.soundEnabled = !app.globalData.soundEnabled;
    app.saveGameData();
    
    wx.showToast({
      title: app.globalData.soundEnabled ? '音效已开启' : '音效已关闭',
      icon: 'success'
    });
  }
})